var config = require('../../utils/config');
var API_URL = config.API_URL;
var login = require('../../utils/login');
var util = require('../../utils/util');
var app = getApp();
let Pay = util.wxPromise(my.tradePay);
var recId;
Page({
    data() { },
    onLoad: function (options) {
        // 页面初始化 options为页面跳转所带来的参数
        recId = options.rec;
        let that = this;
        let params = {
            type: 1,
            cart_id: recId
        }
        util.PRL(API_URL.cart_settle, params)
            .then(res => {
                console.log(res)
                that.setData(res.data.data)
            }).catch(res => {
                console.log(res)
            })
    },
    onReady: function () {
        // 页面渲染完成
    },
    onShow: function () {
        var that = this;
        if (app.globalData.address != undefined) {
            if (that.data.address && that.data.address.addressId != app.globalData.address.addressId) {
                that.setData({
                    address: app.globalData.address
                })
            }
        }
    },
    // 选择地址
    choose_addr: function (e) {
        my.navigateTo({
            url: '/pages/address/address',
        })
    },
    pay: function (e) {
        var that = this;
        if (typeof (that.data.address.addressId) == "undefined") {
            util.showNotice('请选择收货地址');
            return
        }
        util.PRL(API_URL.order, {
            address_id: that.data.address.addressId,
            cart_id: recId,
            leave_comments: that.data.liuyan
        }).then(res => {
            console.log(res);

            return util.POST(API_URL.ali_pay, {
                // tag: 'tianguangxu',
                // userId: userInfo.userId,
                // outTradeNumber: res.data.data.batchNumber,
                // body: res.data.data.goods[0].name,
                // // totalFee: Number(res.data.data.amount) * 100,
                // totalFee: 1,
                // openid: openId,
                // notifyUrl: 'https://mina.jade-box.com/cloudMasses/pay/yudu/notify '
                order_number: res.data.data.orderNumber
            })
        }).then(res => {
            // let timeStamp = res.data.sign.timeStamp.toString();
            return Pay({
                orderStr: res.data.data.url
                // timeStamp: timeStamp,
                // nonceStr: res.data.sign.nonceStr,
                // package: res.data.sign.package,
                // signType: 'MD5',
                // paySign: res.data.sign.sign,
            })
        }).then(res => {
            console.log(res)
            app.globalData.goToOrder = true;
            my.switchTab({
                url: '/pages/user/user'
            })
        }).catch(res => {
            console.log(res);
            if (res.errMsg == 'requestPayment:fail cancel') {
                app.globalData.goToOrder = true;
                my.switchTab({
                    url: '/pages/user/user'
                })
            }
        })
    }
})